import 'package:flutter/material.dart';

class NumberInputWidget extends StatefulWidget {

  int num;
  final double height;
  final double width;
  final ValueChanged<int>? onValueChanged;

  NumberInputWidget({super.key, this.height=24.0,this.width=30.0,this.num = 0, this.onValueChanged});

  @override
  State<NumberInputWidget> createState() => _NumberInputWidgetState();
}

class _NumberInputWidgetState extends State<NumberInputWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(2.0)),
        border: Border.all(color: Colors.grey, width: 0.5),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          GestureDetector(
              onTap: _minusNum,
              child: Icon(Icons.remove)
            //IconButton(icon: Icon(Icons.remove),onPressed: (){},)
          ),
          Container(
            width: 0.5,
            color: Colors.grey,
          ),
          Container(
            width: 32.0,
            alignment: Alignment.center,
            child: FittedBox(
              fit:BoxFit.scaleDown,
                child: Text(widget.num.toString(), maxLines: 1, style: TextStyle(fontSize: 20.0, color: Colors.black),)),
          ),
          Container(
            width: 0.5,
            color: Colors.grey,
          ),
          GestureDetector(
            onTap: _addNum,
            child: Container(
                width: 32.0,
                alignment: Alignment.center,
                child:  Icon(Icons.add)
              //Image.asset(AssetsImages.NUM_ADD), // 设计图
            ),
          ),
        ],
      ),
    );
  }

  void _minusNum() {
    if (widget.num == 0) {
      return;
    }

    setState(() {
      widget.num -= 1;

      if (widget.onValueChanged != null) {
        widget.onValueChanged!(widget.num);
      }
    });
  }

  void _addNum() {
    setState(() {
      widget.num += 1;

      if (widget.onValueChanged != null) {
        widget.onValueChanged!(widget.num);
      }
    });
  }
}
